HTMLCanvasElement = newFunc("HTMLCanvasElement", 1, function HTMLCanvasElement(createObj_key) {
    //   if (createObj_key !== mm.memory.$createObj_key) {
    //     throw new TypeError("Illegal constructor");
    // }
    mm.memory.private_data.set(this, {
        canvas: null
    })
    this.canvas = null;
    // //HTMLElement.call(this)
    // debugger;
})

window['native_toBlob'] = function toBlob() {
    if (debug) print('HTMLCanvasElement.toBlob.call', arguments)

}
Utils.registerNative('toBlob')

window['native_getContext'] = function getContext(contextType) {
    if (debug) print('HTMLCanvasElement.getContext.call', arguments)
    if (!this.context) {
        // debugger
        let context = {};
        switch (contextType) {
            case "webgl":
                this.canvas = cc.createCanvas(this.width, this.height);
                context = new WebGLRenderingContext(mm.memory.$createObj_key, this)
                break;
            case "2d":
                this.canvas = cc.createCanvas(this.width || 150, this.height || 60);
                this.context = this.canvas.getContext('2d')
                // debugger;

                context = new CanvasRenderingContext2D(mm.memory.$createObj_key, this)
                break
            default:
                context = null;
        }
        // debugger;
        try {
            mm.memory.private_data.get(this).context = context
        } catch (e) {

        }
        return context;

        ;
    }

    return this.context;

}
Utils.registerNative('getContext')

window['native_toDataURL'] = function toDataURL(type, encoderOptions) {
    let result = ENV.jpg
    if (this.width <= 10 && this.height <= 10) {
        result = ENV.jpg2.url
    }
    // if (this.width === 4 && this.height === 4) {
    //     // result = ""
    //             result = ENV.jpg2.url
    //
    // }
    // debugger
    let res = result.split(';');
    // debugger
    if (type) {
        result =  'data:' + type + ';' + res[1];

    }
    // else{
    //     result = res;
    // }
    if (debug) print('HTMLCanvasElement.toDataURL.call',[type, encoderOptions], this.width, this.height, result)

    return result
}
Utils.registerNative('toDataURL')

;(function () {
    const $safe_get_attribute = ['height', 'width'];
    const $safe_set_attribute = ['height', 'width'];
    const $safe_func_attribute = ['captureStream', 'getContext', 'toBlob', 'toDataURL', 'transferControlToOffscreen'];

    Object.defineProperties(HTMLCanvasElement.prototype, {
            [Symbol.toStringTag]:{value:'HTMLCanvasElement'},

        width: {

            get: newFunc('width', 10, function width() {
                // debugger;
                let res = mm.memory.private_data.get(this).width  || 300
                if (debug) print('HTMLCanvasElement.width.get', res)

                return res;
            }, {get: true}),

            set: newFunc('width', 1, function width(value) {
                if (debug) print('HTMLCanvasElement.width.set', value)
                mm.memory.private_data.get(this).width = value

            }, {set: true})


        },
        height: {

            get: newFunc('height', 10, function height() {
                let res = mm.memory.private_data.get(this).height || 300
                if (debug) print('HTMLCanvasElement.height.get', res)

                return res;
            }, {get: true}),

            set: newFunc('height', 1, function height(value) {
                if (debug) print('HTMLCanvasElement.height.set', value)
                mm.memory.private_data.get(this).height = value

            }, {set: true})


        },
        captureStream: {

            value: newFunc('captureStream', 10, function captureStream() {
                if (debug) print('HTMLCanvasElement.captureStream.call', arguments)

            })


        },
        getContext: {

            value: window['getContext']


        },
        toBlob: {

            value: window['toBlob']
            , writable: true, enumerable: true, configurable: true,


        },
        toDataURL: {

            value: window['toDataURL']
            , writable: true, enumerable: true, configurable: true,


        },
        transferControlToOffscreen: {

            value: newFunc('transferControlToOffscreen', 10, function transferControlToOffscreen() {
                if (debug) print('HTMLCanvasElement.transferControlToOffscreen.call', arguments)

            })


        },
        constructor: {

            value: newFunc('constructor', 10, function constructor() {
                if (debug) print('HTMLCanvasElement.constructor.call', arguments)

            })


        },
    })

    // mm.rename(HTMLCanvasElement.prototype, "HTMLCanvasElement");
    // mm.safeDescriptor_addConstructor(HTMLCanvasElement);
    // mm.safe_Objattribute(HTMLCanvasElement, $safe_get_attribute, $safe_set_attribute, $safe_func_attribute);
    // HTMLCanvasElement.prototype.constructor = HTMLElement.constructor
    Object.setPrototypeOf(HTMLCanvasElement.prototype, HTMLElement.prototype);
    Object.setPrototypeOf(HTMLCanvasElement, HTMLElement);
    // HTMLCanvasElement.__proto__ = HTMLElement.prototype

})();


mm.memory.htmlElements["canvas"] = function () {
    return new HTMLCanvasElement(mm.memory.$createObj_key);
}


